package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;


// Created by C4 ,  Arwa Ismail

@With(Secure.class)					//this class is used for the login, Doaa El-Hawary
public class Ratings extends Controller{
	 
	public static void index() {
	        render();
	    }


	/**
		 * Creates a rating to be added by a user to an object, mainly idea/plan/request, 
		 * calculates the new average rating to be shown.
		 *
		 * @author      Arwa Ismail
		 * @version     2010.0331                            (E.g. ISO 8601 YYYY.MMDD)
		 * @category   C4S8
		 * @params     username: username of the user currently connected
		 * 				rating: the rating entered by the user	
		 * 				type: type of model the user is currently viewing
		 * 				modelID: the ID of the currently being viewed model
		 */
	
	public static void addRating(String username, String rating, String type, long modelID)
	{	
		
		User rater = User.findByUsername(username);
		
		Rating rate = new Rating(rater, rating, type, modelID);
		rate.save();
		if (type.equalsIgnoreCase("Idea")) {
			Idea idea = Idea.findById(modelID);
			idea.getRatings().add(rate);
			idea.calculateAverageRating();
			idea.save();
			render(idea);
		} else

		{
			if (type.equalsIgnoreCase("Plan")) {
				Plan plan = Plan.findById(modelID);
				plan.getRatings().add(rate);
				plan.calculateAverageRating();
				plan.save();
				render(plan);
			} else {
				if (type.equalsIgnoreCase("Request")) {
					Request request = Request.findById(modelID);
					request.getRatings().add(rate);
					request.calculateAverageRating();
					request.save();
					render(request);
				}
			}
		}

		render();

	}
	

}
